require('../css/common/normalize.css')
require('../css/common/reset.css')
require('../css/common/property.css')
require('../css/common/basic.less')
require('../lib/fonts/iconfont.css')
require('../css/motion-train.less') 

//导入axios
const axios = require("axios");

//导入weui
require("weui")
const weui = require("weui.js")

//导入util
const util = require('../util/util')

 /**
* 0.底部导航
*/
util.fixBottomNav('motion-run')

//最新课程
// 获取元素
let imgurl=document.querySelector('#imgurl')
let name=document.querySelector('#name')
let desc=document.querySelector('#desc')
let trainKe=document.querySelector('#train-ke')
let train2=document.querySelector('#train2')
let train1=document.querySelector('.train1')

axios.get('http://139.9.177.51:8099/sports/allcourse').then(function(res){
    console.log(res.data.data);
    let course=res.data.data
    //console.log(res.data);

    //最新课程
   let coureNew= course.filter(function(item){
        return item.latest==1
    })
    imgurl.src="http://139.9.177.51:8099"+coureNew[0].imgurl
    name.textContent=coureNew[0].name
    desc.textContent=coureNew[0].desc
    train1.dataset.cid=coureNew[0].courseId


    //课程列表
    let coureList= course.filter(function(item){
        return item.latest==0
    })  

    let arr=coureList.map(function(item){
        return`
        <li data-bid="${item.courseId}">
                <img src="http://139.9.177.51:8099${item.imgurl}" alt="" >
                <h5 id="title">${item.name}</h5>
                <p id="main">${item.desc}</p>
        </li>
        
        `
    })

    console.log(arr);  //得到的是数组，我们要将他分成字符串
    train2.innerHTML=arr.join('')

// 建立点击事件，点击的时候将自定义属性的值存储到临时存储，并且跳转

//最新课程
train1.addEventListener('click',function(){

    //localStorage.setItem('userId',res.data.data.user.userId)
    //将自定义属性值存储到临时存储
   sessionStorage.setItem('cid',train1.dataset.cid)

    //跳转页面
    location.href='./motion-course.html'
    
})

//课程列表  运用事件委派原理
train2.addEventListener('click',function(e){

    //localStorage.setItem('userId',res.data.data.user.userId)
    //将自定义属性值存储到临时存储
    let bid=e.target.closest('li').dataset['bid']  //获取：元素节点对象.dataset.属性名
    sessionStorage.setItem('bid',bid)
    location.href='./motion-course.html'

})

})